<template>
  <div>
    <div class="flex">
      <span class="font-size-16 color-333333 padding-top-5 required">上传图片</span>
      <UploadPhotoList :key="guid()" class="upload-box" ref="photoAlbum" :multiple="true" :fileList="photoList" @successChange="setPhotoList" @deleteChange="setPhotoList"></UploadPhotoList>
    </div>
    <div class="flex">
      <span class="font-size-16 color-333333 padding-top-5 required">上传附件</span>
      <upload-file :multiple="true" class="upload-box" :limit="10" tip="" :max-size="100" accept=".xlsx,.xls,.csv,.png,.jpg,.Png,.Jpg,.pdf" :fileList.sync="fileList"> </upload-file>
    </div>
  </div>
</template>

<script>
  import { guid } from '@/common/js/utils';
  export default {
    name: 'addWork',
    props: {
      isComment: {
        type: Boolean,
        default: false,
      },
    },

    data() {
      return {
        photoList: [],
        fileList: [],
        guid:guid
      };
    },

    methods: {
      init() {
        this.photoList = [];
        this.fileList = [];
      },

      //图片list
      setPhotoList(data) {
        this.photoList = data.dataList;
      },

      getOtherList() {
        this.photoList.forEach(item => {
          item.fileId = item.key;
          item.fileUrl = item.url;
          item.fileTitle = item.originalName;
        });
        return this.photoList
      },

      getPhotoCommentList() {
        this.photoList.forEach(item => {
          item.primaryKey = item.key;
          item.attachmentUrl = item.url;
          item.attachmentName = item.originalName;
        });
        return this.photoList
      },

      getPhotoList() {
        if (this.isComment) {
          return this.getPhotoCommentList();
        } else {
          return this.getOtherList();
        }
      },

      // 设置附件列表 
      getFileList() {
        let list = [];
        this.fileList.forEach(item => {
          if (item.response && item.response.data.rows.length > 0) {
            let fileObj = item.response.data.rows[0];
            list.push({
              fileId: fileObj.key,
              fileUrl: fileObj.url,
              fileTitle: item.name,
            });
          } else {
            list.push({
              fileId: item.key,
              fileUrl: item.url,
              fileTitle: item.name,
            });
          }
        });
        return list;
      },

      getFileCommentList() {
        let list = [];
        this.fileList.forEach(item => {
          if (item.response && item.response.data.rows.length > 0) {
            let fileObj = item.response.data.rows[0];
            list.push({
              primaryKey: fileObj.key,
              attachmentUrl: fileObj.url,
              attachmentName: item.name,
            });
          }
        });
        return list;
      },

      //回显赋值
      backInfo (photoList,fileList) {
        this.photoList = photoList
        this.fileList = fileList
      }
    },
  };
</script>

<style lang="scss" scoped>
  .upload-box {
    margin-left: 44px;
  }
</style>
